package cn.changeforyou.analyzer.javaparserDemo;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.ConstructorDeclaration;
import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class CompareTwoJavaFile extends BaseTest{
	
	List<Node> differentNodes = new ArrayList<>();
	
	@Test
	public void test() throws UnsupportedEncodingException, FileNotFoundException {
		CompilationUnit c1 = getCompilationUnit("D:\\code\\analyzer\\analyzer\\src\\main\\resources\\srcFiles\\t1\\HelloWorld.java");
		CompilationUnit c2 = getCompilationUnit("D:\\code\\analyzer\\analyzer\\src\\main\\resources\\srcFiles\\t2\\HelloWorld.java");
		
		System.out.println(c1.equals(c2));
		Optional<ClassOrInterfaceDeclaration> helloWorld1 = c1.getClassByName("HelloWorld");
		ConstructorDeclaration d1 = helloWorld1.get().getDefaultConstructor().get();
		Optional<ClassOrInterfaceDeclaration> helloWorld2 = c1.getClassByName("HelloWorld");
		ConstructorDeclaration d2 = helloWorld2.get().getDefaultConstructor().get();
		System.out.println(d1.equals(d2));
		
	}
	
	public void add(Node n1) {
		List<Node> parentsNode = new ArrayList<>();
		Optional<Node> parentNode_op = n1.getParentNode();
		while(parentNode_op.isPresent()) {
			Node parent = parentNode_op.get();
			parentsNode.add(parent);
			parentNode_op = parent.getParentNode();
		}
		
		differentNodes.removeAll(parentsNode);
		differentNodes.add(n1);
	}
	
	public void findDif(Node n1, Node n2) {
		if (!n1.equals(n2)) {
			add(n1);
			List<Node> childNodes = n2.getChildNodes();
		}
	}
}
